﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Common;
using Dto;

namespace SqlServerDAL
{
    public class UserAlbumDAL
    {
        private const string Waming = "SqlServerDAL.UserAlbumDAL";

        public bool Save(UserAlbumDto dto)
        {
            try
            {
                string sql;
                var parameters = new List<SqlParameter>
                                     {
                                         new SqlParameter("@AlbumName", SqlDbType.VarChar) {Value = dto.AlbumName},
                                         new SqlParameter("@AlbumUserId", SqlDbType.Int) {Value = dto.AlbumUserId},
                                         new SqlParameter("@AlbumTime", SqlDbType.DateTime) {Value = dto.AlbumTime}
                                     };
                if (dto.AlbumId == 0)
                {
                    sql =
                        "insert into UserAlbum(AlbumName,AlbumUserId,AlbumTime) values(@AlbumName,@AlbumUserId,@AlbumTime)";
                }
                else
                {
                    sql =
                        "update UserAlbum set AlbumName=@AlbumName,AlbumUserId=@AlbumUserId,AlbumTime=@AlbumTime where AlbumId=@AlbumId";
                    parameters.Add(new SqlParameter("@AlbumId", SqlDbType.Int) { Value = dto.AlbumId });
                }
                SqlHelper.ExecuteNonQuery(SqlHelper.connStr, CommandType.Text, sql, parameters.ToArray());
                return true;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".Save(UserAlbumDto dto)", ex);
                return false;
            }
        }

        public List<Hashtable> FindAll(int userId)
        {
            try
            {
                var hts = new List<Hashtable>();
                const string sql = "select * from UserAlbum ";
                var parameters = new List<SqlParameter>();
                var whereSql = "";
                if (userId != 0)
                {
                    whereSql = " where AlbumUserId=@AlbumUserId";
                    parameters.Add(new SqlParameter("@AlbumUserId", SqlDbType.Int) { Value = userId });
                }
                using (var red = SqlHelper.ExecuteReader(SqlHelper.connStr, CommandType.Text, sql + whereSql, parameters.ToArray()))
                {
                    while (red.Read())
                    {
                        hts.Add(SqlHelper.FillHashtable(red));
                    }
                }
                return hts;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".FindAll(int userId)", ex);
                return null;
            }
        }

        public Hashtable FindById(int id)
        {
            try
            {
                Hashtable ht = null;
                const string sql = "select * from UserAlbum where AlbumId=@AlbumId";
                var param = new SqlParameter("@AlbumId", SqlDbType.Int) { Value = id };
                using (var red = SqlHelper.ExecuteReader(SqlHelper.connStr, CommandType.Text, sql, param))
                {
                    if (red.Read())                                                                         
                    {
                        ht = SqlHelper.FillHashtable(red);
                    }
                }
                return ht;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".FindById(int id)", ex);
                return null;
            }
        }
    }
}
